<template>
    <div>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="题目" prop="topicTitle" style="width: 600px;">
                <el-input type="textarea" v-model="form.topicTitle"></el-input>
            </el-form-item>

            <el-form-item label="语言" prop="topicTypeId">
                <el-select v-model="form.topicTypeId" placeholder="请选择语言">
                    <el-option v-for="item in topicType" :key="item.id" :label="item.name" :value="item.id * 1">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="题目难度">
                <el-radio-group v-model="form.topicComplexity">
                    <el-radio label="1" name="type">简单</el-radio>
                    <el-radio label="2" name="type">一般</el-radio>
                    <el-radio label="3" name="type">困难</el-radio>
                </el-radio-group>
            </el-form-item>

            <el-form-item label="答案" prop="topicAnsWer">
                <el-input type="textarea" v-model="form.topicAnswer" style="width: 600px;"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="topicAdd('form')">立即创建</el-button>
                <el-button @click="resetForm('form')">重置</el-button>
                <el-button @click="cancelAdd()">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
export default {
    name: "TopicAdd",
    data() {
        return {
            topicType: [],
            form: {
                topicTypeId: "",
                topicTitle: "",
                topicComplexity: "",
                topicAnswer: "",
            },
            rules: {
                topicTitle: [
                    { required: true, message: '请输入题目名称', trigger: 'blur' },
                ],
                topicTypeId: [
                    { required: true, message: '请选择语言类型', trigger: 'change' }
                ],
                topicComplexity: [
                    { required: true, message: '请选择难度', trigger: 'blur' }
                ],
                topicAnswer: [
                    { required: true, message: '请输入答案', trigger: 'blur' }
                ],
            },
        }
    },
    methods: {
        cancelAdd() {
            this.$router.forward();
        },
        load() {
            this.request.get("topic_type/list").then(res => {
                this.topicType = res.data
            })
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        topicAdd(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.request.post("topic_audit/save", this.form).then(res => {
                        this.$router.push("/topic_audit")
                    })
                } else {
                    return false;
                }
            });
        },
    },
    created() {
        this.load();
    },

}
</script>